$(function(){
    $(".zwl-closep").on("click",function(){
        $(".zwl-pay").css("display","none");
        $('html,body').css({"height":"auto","overflow":"auto"});
    });
    var value="0";
    var doc=document.documentElement||document;     
    function toggleInfo(tagElement,displayStr,overflowStr){
        $(tagElement).on("click",function(){
              value="0"
              $("#shop-regi").css("display",displayStr);
              $("#content").css("display",displayStr);  
              $(".detail-reg-l").attr("data-data",value);
              if(value==0){
                doc.style.overflow=overflowStr;
              }
        });  
    }
    toggleInfo(".detail-reg-l","block","hidden");
    toggleInfo("#shop-regi","none","visible");
    //获取url上？后面参数的值
    var productId=SYS_GLOBAL_CONFIG.getQueryString("productId");
    var shopId="";
    var locX;
    var locY;
    var url=SYS_GLOBAL_CONFIG.shaUrl,
        asyn=true,
        type="post";
    var userId;
   var requestData=JSON.stringify({"code":"getProductDetail","version":"1.0","data":{"productId":productId,"userId":userId}});  
        getData(url,requestData,call_func1,asyn,type);  
    //收藏店铺
    var requestData3="";
    var requestData5="";
    var requestData4="";
    var requestData6="";
    var requestData7="";
    
    /**
     *通过不同的请求参数，获取数据（公共的ajax获取数据方法）
     */
    function getData(url,requestData,call_func,asyn,type){
        console.log(requestData+"请求")
            $.ajax({
                url:url,

                type:type,

                datatype:"json",

                contentType:"application/json",

                data:requestData,
                
                async:asyn,

                success:function(data){
                    call_func(data);
      
                },
                error:function(msg){
                    alert("错误信息:"+"请刷新页面");
                }   
        
            });        
        }  
    var colFlag=0;
    //产品信息(回调)
    function call_func1(data){
       console.log(data);
       var datas=data.data.product;
       var dataval=data.data.shop;
       $(".detail-name-l .p1").html(datas.productName);
       $(".detail-name-l .p2").html("￥"+datas.saleprice+"/"+datas.unit);
       $(".introduce-l .img3").attr("src",dataval.logoImgPath)
       $(".introduce-name").html(dataval.shopName);
       $(".addr-p").html("地址："+dataval.address);
       $(".time-p").html("营业时间："+dataval.openTime+"-"+dataval.closeTime);
       $(".tel").html(dataval.phoneNO);
       $(".num-top1").html(dataval.shopCollectCount);
       $(".num-top2").html(dataval.viewCount);
       $(".num-top3").html(dataval.shopProductsCount);
       $(".person-p1").html(dataval.remark);
       var show1='<div class="pic-show"><div class="pic-show-l"><img class="person-img" src="'+datas.fullPath+'" alt=""></div></div>';
       var shopImgs=data.data.product.productImgs;
       var shopImgLen=shopImgs.length;
       var isCollect=data.data.shop.isCollect;//店铺收藏
       var isCollectPro=data.data.product.isCollect;//产品收藏
       var isAvaliable=data.data.product.sellerFees.length;
       var btnFlag=0;
        console.log(isCollect+"获取到的isCollect")
       //图片详情
       if(shopImgLen>0){
            for(var i=0;i<datas.productImgs.length;i++){
                  if(shopImgs[i].FULLPATH!=null){
                        datafullpath=shopImgs[i].FULLPATH
                        pathLen=datafullpath.length;
                        lastIndex=datafullpath.lastIndexOf(".");
                        str1=datafullpath.substr(0,lastIndex);
                        str2=datafullpath.substr(lastIndex,pathLen);;
                        fullpath=str1+"_max"+str2;
                        show1+=' <div class="pic-show"><div class="pic-show-l"><img class="person-img" src="'+fullpath+'" alt=""></div></div>'      
                  }
            }
           
       }else{
           $(".pic-show").css("height","12rem");
            $(".pic-show-l .person-img").css("display","none");
       }
       $(".pic-shower").html(show1);

        
        $(".foot1 .ms").attr("href","sms:"+dataval.phoneNO);
        $(".foot1 .phone").attr("href","tel:"+dataval.phoneNO);
        //图片详情轮播
        var wStrs="";
        var hStrs="";
        var img_s=new Image();
        img_s.src=datas.fullPath;  
        if(datas.fullPath==null||datas.fullPath=="null"){
            img_s.src=" ../images/error.png";
        }
        
        var shopImgsStr='<li class="l"><a href="javascript:void(0);"><img width="" height="" _src="'+datas.fullPath+'" src="./images/load.gif" /></a></li>';
        img_s.onload = function(){    
            if((img_s.width/img_s.height)<1){
                wStrs="16rem";
                hStrs="auto";
            }else{
                wStrs="auto";
                hStrs="16rem";
            }
            $(".bd ul").find("img").css("width",wStrs);
            $(".bd ul").find("img").css("height",hStrs); 
        }
        var lastIndex=0;
        var datafullpath="";
        for(var i=0;i<shopImgLen;i++){
            if(shopImgs[i].FULLPATH!=null&&i<2){ 
                datafullpath=shopImgs[i].FULLPATH
                pathLen=datafullpath.length;
                lastIndex=datafullpath.lastIndexOf(".");
                str1=datafullpath.substr(0,lastIndex);
                str2=datafullpath.substr(lastIndex,pathLen);;
                fullpath=str1+"_max"+str2;
                var str="";
                var wStr="";
                var hStr="";
                a(i);
                function a(i){
                    var img=new Image();
                    var i=i;
                    img.src=fullpath;  
                    if(fullpath==null||fullpath=="null"){
                        img.src=" ../images/error.png";
                    }
                    img.onload = function(){    
                        if((img.width/img.height)<1){
                            wStr="16rem";
                            hStr="auto";
                        }else{
                            wStr="auto";
                            hStr="16rem";
                        }
                       $(".bd ul").find("img")[i].style.width=wStr;
                       $(".bd ul").find("img")[i].style.height=hStr; 
                       
                };
                shopImgsStr+='<li class="l"><a href="javascript:void(0);"><img width="" height="" _src="'+fullpath+'" src="./images/load.gif" /></a></li>';
            }
        }
        }
        $(".bd ul").html(shopImgsStr);
        var l=$(".bd li img").length;
        for(var i=0;i<l;i++){
                var img=new Image();
                img.src=$(".bd li img")[i].getAttribute("_src");
            }
        TouchSlide({ 
            slideCell:"#focus",
            titCell:".hd ul", //开启自动分页 autoPage:true ，此时设置 titCell 为导航元素包裹层
            mainCell:".bd ul", 
            effect:"left", 
            autoPlay:true,//自动播放
            autoPage:true, //自动分页
            switchLoad:"_src" //切换加载，真实图片路径为"src" 
        });
      
        shopId=dataval.shopId;
        console.log(shopId+"shopId");
        var data1=JSON.stringify({"data":{"shopId":shopId},"version":"1.0","code":"getShopDetail"});
        $(".introduce-a .introduce-l,#intoShop").on("click",function(){
            location.href="./shopDetail.html?shopId="+shopId;
        });
      
        //配送or支付
        if(isAvaliable>0){
            var tagStr="";
            var tag="";
            var tagLen=data.data.tagServices.length;
            for(var i=0;i<2;i++){
                tagStr+='<img class="img1" src="'+data.data.tagServices[i].tagIconPath+'" alt=""><span class="span1">'+data.data.tagServices[i].userRemark+'</span>';    
            }
            $(".distri-pay").append(tagStr);
        }else{
            $(".distri-pay").css("display","none");
        }      
        
       
       
        $(".distri-pay").on("click",function(){
             $(".zwl-pay").css("backgroundImage","url("+data.data.shop.logoImgPath+")" );
        $(".zwl-pay").css("display","block");
         $('html,body').css({"height":"100%","overflow":"hidden"});
        requestData7=JSON.stringify({"code":"getProductDetail","version":"1.0","data":{"productId":productId,"userId":userId}});
         getData(url,requestData7,call_func7,asyn,type);  
    });
        
    function call_func7(data){
        var tagServices=data.data.tagServices.length;
        var isAvaliableStr="";
        $(".zwl-shopname").html(data.data.shop.shopName);
        for(var i=0;i<tagServices;i++){
            isAvaliableStr+='<p class="zwl-lp"><img class="zwl-lpimg" src="'+data.data.tagServices[i].tagIconPath+'" alt=""><span class="zwl-lpdetail">'+data.data.tagServices[i].userRemark+'</span></p>';
            $(".zwl-pp").html(isAvaliableStr);
        } 
    }  
    //认证资料（回调）
    function call_func2(data){
        var dataval2=data.data.shop;
        $(".big-image").css("backgroundImage","url("+dataval2.logoImgPath+")");
        $(".small-images").attr("src",dataval2.logoImgPath);
        $(".shop-name .name").html(dataval2.shopName); 
        $(".resource-show-r").html("");
        var regStr="";
        if(data.data.seller.isSellerAuth==0){
            regStr="";
        }else{
            regStr='<div class="detail-reg-l"><div class="detail-reg-r"><span class="span2">商家认证资料</span><img class="img2" src="images/reg_back.png" alt=""></div></div>';
        
        }
        for(var i=0;i<data.data.authImgs.length;i++){

            var shop='<div class="open">'+
                  '<p class="open-l"><img class="open-l-image" src="images/xuanzhong_pre.png" alt=""><span class="span-r">'+data.data.authImgs[i].imageTittle+'</span></p>'+
                  '<div class="resource-pic">'+
                      '<img class="resource-pic1" src="'+data.data.authImgs[i].FULLPATH+'" alt=""></div>'+
                  '<div class="resource-pic-r">'+
                       '<img class="resource-pic1-r" src="'+data.data.authImgs[i].FULLPATH+'" alt=""></div><div class="click"> 点击查看</div></div>';
            $(".resource-show-r").append(shop);
        }
        $(".click").on("click",function(){

            $(this).siblings().css("display","block");

        });
         $(".resource-pic-r").on("click",function(){
             $(this).css("display","none");
        });
   }  
    /**
     *获取认证资料信息
     */
        $(".span2").on("click",function(){
            getData(url,data1,call_func2,asyn,type);
        });
        
    }
});